// MyAxios.js
import axios from 'axios'
import router from '@/router/index';
import { MessageBox, Message, Loading } from "element-ui";
import store from '@/store/index'
// import store from '@/store'
// 创建axios实例
const instance = axios.create({
    timeout: 30000,
})
var url
let number
let loadingInstance
instance.interceptors.request.use(function (config) {
    url = config.url.split('/')[config.url.split('/').length - 1]
    if (url == 'getUserInfo') {
        localStorage.setItem('cont', localStorage.getItem('cont') * 1 + 1 * 1)
        number = localStorage.getItem('cont')
    }
    loadingInstance = Loading.service();
    config.headers['content-type'] = "application/json"
	config.headers['X-Frame-Options'] = 'DENY'
    // config对象中保存着请求对象的配置信息，此处可以进行修改
    if (localStorage.getItem('token')) {
        config.headers['Authorization'] = localStorage.getItem('token')
    }
    return config
}, err => {
    return Promise.reject(err.response);
}
)
instance.interceptors.response.use(res => {
    loadingInstance.close();

    if (res.data.code == 200) {
        return res
    }
    switch (res.data.code) {
        case 401:
            if (url == 'getUserInfo' & number == 1) {
                localStorage.removeItem('token')
                localStorage.removeItem('cont')
            } else {
                Message({
                    message: '登录超时，请重新登录',
                    type: "error",
                });
                sessionStorage.removeItem('NavStyleState') //清空导航菜单选中状态记录  //该记录用于刷新页面时 导航菜单样式选中不丢失
                setTimeout(() => {
                    router.replace({
                        path: "/Login"
                    })
                }, 1000)
            }

            break;
        case 403:
            Message({
                message: res.data.msg,
                type: "error",
            });
            break;
        case 404:
            Message({
                message: '资源，服务未找到',
                type: "error",
            });
            break;
        case 500:
            Message({
                // message: '系统内部错误',
                //如果服务器没有显示数据，则返回默认错误消息
                message: (res.data.msg === null || res.data.msg === undefined || res.data.msg === "" ? "系统内部错误" : res.data.msg),
                type: "error",
            });
            break;
        case 601:
            Message({
                message: res.data.msg,
                type: "error",
            });
            break;
    }
    return res


}, error => {
    return Promise.reject(error);
})


let myaxios = {
    /**
     * 发送get请求
     * @param {string} url  请求路径
     * @param {object} params  请求参数（对象的形式传递）
     * @returns Promise
     */
    get(url, params) {
        return instance({
            url,
            method: 'GET',
            params,
        })
    },

    /**
     * 发送post请求
     * @param {string} url  请求路径
     * @param {object} params  请求参数（对象的形式传递）
     * @returns Promise
     */
    // application/json; charset=utf-8
    post(url, params) {
        return instance({
            url,
            method: 'POST',
            data: JSON.stringify(params)
        })
    },
    Delete(url, params) {
        return instance({
            url,
            method: 'DELETE',
            params,
        })
    },

    /**
     * 发送put请求
     * @param {string} url  请求路径
     * @param {object} params  请求参数（对象的形式传递）
     * @returns Promise
     */
    put(url, params) {
        return instance({
            url,
            method: 'PUT',
            data: JSON.stringify(params)
        })
    }

}





export default myaxios;